package org.datascooter.cglib.proxy.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.datascooter.cglib.proxy.ObjectWrapper;
import org.datascooter.cglib.proxy.SimpleInterceptor;
import org.datascooter.exception.EntityNotMappedException;
import org.datascooter.inface.IDataManager;

public class ProxyList<T> extends SimpleInterceptor implements List<T> {

	private static final long serialVersionUID = 5009508959241634349L;
	private List<T> list;

	public ProxyList(SimpleInterceptor parent, Object list, String listAttribute, IDataManager dataManager) {
		super(parent, list, listAttribute, dataManager);
	}

	// @Override
	// public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	// return method.invoke(list, args);
	// }
	@Override
	public Object getSource() {
		return list;
	}

	@Override
	protected void saveContent(String attributeName, Object[] args) throws EntityNotMappedException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
		super.saveContent(attributeName, args);
		list.add((T) args[0]);
	}

	@Override
	public void setContainer(Object object) {
		container = object;
		list = crossProvider.getContent(object, containerAttribute);
		for (T item : list) {
			try {
				ObjectWrapper.wrap(manager, item);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	public int size() {
		return list.size();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public Iterator<T> iterator() {
		return list.iterator();
	}

	public Object[] toArray() {
		return list.toArray();
	}

	@SuppressWarnings("hiding")
	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}

	public boolean add(T e) {
		return list.add(e);
	}

	public boolean remove(Object o) {
		return list.remove(o);
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public boolean addAll(Collection<? extends T> c) {
		return list.addAll(c);
	}

	public boolean addAll(int index, Collection<? extends T> c) {
		return list.addAll(index, c);
	}

	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	public void clear() {
		list.clear();
	}

	public boolean equals(Object o) {
		return list.equals(o);
	}

	public int hashCode() {
		return list.hashCode();
	}

	public T get(int index) {
		return list.get(index);
	}

	public T set(int index, T element) {
		return list.set(index, element);
	}

	public void add(int index, T element) {
		list.add(index, element);
	}

	public T remove(int index) {
		return list.remove(index);
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<T> listIterator() {
		return list.listIterator();
	}

	public ListIterator<T> listIterator(int index) {
		return list.listIterator(index);
	}

	public List<T> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}
}
